package com.topideal.supplychain.hades.master.mapper;

import java.util.List;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import com.topideal.supplychain.hades.master.model.Carrier;
import com.topideal.supplychain.hades.master.sql.CarrierSqlProvider;

/**
 * 
 * TODO
 * 
 * @author huanghonglin
 * @date 2017年9月26日 上午9:25:26
 */
@Mapper
public interface CarrierMapper {
    /**
     * 
     * @desc 查询所有
     * @date 2017年9月26日 上午9:25:35
     * @return
     */
    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where is_deleted='0' order by id asc")
    List<Carrier> findAll();

    /**
     * 
     * @desc 由id找name
     * @date 2017年9月26日 上午9:26:04
     * @param name
     * @return
     */
    @Select("Select  id" + " from md_carrier where name=#{name} limit 1")
    Long findIdByName(String name);

    /**
     * 
     * @desc 条件查询
     * @date 2017年9月26日 上午9:26:30
     * @param carrier
     * @return
     */
    @SelectProvider(type = CarrierSqlProvider.class, method = "getSqlBySelective")
    List<Carrier> findBySelective(Carrier carrier);


    /**
     * 
     * @desc 根据条件插入
     * @date 2017年9月26日 上午9:27:37
     * @param filter
     * @return
     */
    @InsertProvider(type = CarrierSqlProvider.class, method = "insertSelective")
    int insertCarrier(Carrier filter);

    // 个别子都如is_deleted,version没有查询。
    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where is_deleted='0' and id=#{carrierId}")
    Carrier get(Long carrierId);

    /** 根据条件更新 */
    @UpdateProvider(type = CarrierSqlProvider.class, method = "updateSelective")
    int updateCarrier(Carrier filter);

    /** 删除 */
    @Update("update md_carrier set is_deleted='1' where id=#{id}")
    int delete(String id);

    /** 修改状态 */
    @Update("update md_carrier set state=#{state} where id=#{id}")
    int updateState(@Param("state") int state, @Param("id") String id);

    List<Carrier> list(Carrier record);

    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where is_deleted='0' and out_carrier_code=#{outCarrierCode}")
    Carrier selectByOutCarrierCode(String outCarrierCode);

    Carrier selectByCode(String carrierCode);

    /**
     * @author wanwei
     * @TODO
     * @date: 2017年11月9日 上午10:08:10
     */
    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where is_deleted='0' and kdniao_code=#{kdniaoCode}")
    Carrier findByKdniaoCode(String kdniaoCode);

    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by "
            + "from md_carrier where is_deleted='0' and code=#{carrierCode}")
    Carrier findByCode(String carrierCode);

    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where code =  (select carrier_code from md_carrier_mapping where out_carrier_code=#{carrierCode} and subscriber_code =#{subscriberCode}) and is_deleted='0'")
    Carrier findByMappingOutCodeAndSubscriberCode(@Param("subscriberCode")String subscriberCode,@Param("carrierCode")String carrierCode);

    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where out_carrier_code =#{carrierCode} and is_deleted='0'")
    Carrier findByOutCarrierCode(String carrierCode);

    @Select("select id,code,CARRIER_LEVEL as level,name,"
            + "kd100_code as kd100Code,kdniao_code as kdniaoCode,out_carrier_code as outCarrierCode,"
            + "state,create_time as createTime,"
            + "create_by as createBy,update_time as updateTime,update_by as updateBy "
            + "from md_carrier where is_deleted='0' and kd100_code=#{kd100Code}")
	Carrier findByKd100Code(String kd100Code);
}
